package com.it;

import com.it.bean.R;
import com.it.bean.RestStatusEnum;
import com.it.pojo.params.user.UserCenterInfoQuery;
import com.it.pojo.vo.user.UserCenterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("user")
@CrossOrigin
@Api(value = "v2", tags = "用户相关接口")

public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "根据用户id查询前台用户空间的数据", notes = "根据用户id查询前台用户空间的数据")

    @GetMapping("{userId}")
    public R<?> getUserVO(@PathVariable("userId") Integer userId) {
        if (userId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        Map<String, Object> map = userService.getUserVO(userId);
        return R.success("成功", map);
    }

    @GetMapping("/center/{userId}")
    @ApiOperation(value = "根据用户id查询个人中心的数据", notes = "根据用户id查询个人中心的数据")
    public R<?> getUserCenter(@PathVariable("userId") Integer userId) {
        if (userId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        System.out.println("userId = " + userId);
        UserCenterVO userCenterVO = userService.getUserCenterVO(userId);
        return R.success("成功", userCenterVO);

    }

    @PutMapping("/center/info")
    @ApiOperation(value = "根据用户id修改用户个人基本信息", notes = "根据用户id修改用户个人基本信息")
    public R<?> updateInfo(@RequestBody UserCenterInfoQuery userCenterInfoQuery) {
        if (userCenterInfoQuery == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        System.out.println("userCenterInfoQuery = " + userCenterInfoQuery);
        int i = userService.updateUserCenterInfo(userCenterInfoQuery);
        return i > 0 ? R.success() : R.error();
    }

    @PutMapping("/center/info/pwd")
    public R<?> updatePwd(@RequestParam("userId") Integer userId, @RequestParam("password") String password
            , @RequestParam("rePwd") String rePwd
    ) {
        if (StringUtils.isEmpty(password) || userId == null || StringUtils.isEmpty(rePwd)) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        int i = userService.updatePwd(userId, password, rePwd);
        return i > 0 ? R.success() : R.error();
    }

}
